//
//  CarCollectionViewController.m
//  YouWen
//
//  Created by liqiang－pc on 15/3/12.
//  Copyright (c) 2015年 刘永和. All rights reserved.
//

#import "CarCollectionViewController.h"

@interface CarCollectionViewController ()

@end

@implementation CarCollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blackColor];
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height - 64)] ;
    
    [self.view addSubview:self.myTableView];
    self.myTableView.dataSource = self;
    self.myTableView.delegate = self;
    //    self.myTableView.separatorColor = [UIColor B];
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    
    [self.myTableView setEditing:editing animated:animated];
}


- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  UITableViewCellEditingStyleDelete;
    
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

//设置section个数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    [[DataBaseHandle shareDataBaseHandle] openAndCreatDataBase];
    return [[[DataBaseHandle shareDataBaseHandle] selectCar] count];
    
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //1.删数据库表内容
    [[DataBaseHandle shareDataBaseHandle] openAndCreatDataBase];
    //要删除的model对象
    Model *model = [[[DataBaseHandle   shareDataBaseHandle] selectCar] objectAtIndex:indexPath.row];
    
    [[DataBaseHandle shareDataBaseHandle]deleteCar:model];
    
    //2.删除cell内容
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationLeft];
    
}



//创建cell 并设置cell上内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //一个重用池里可能有多个不同的cell,为了区别不同的cell,要给cell设置重用标示符
    //1.定义cell重用标示符
    static NSString *cellIdentifier = @"cell";
    //2.从重用池里取出cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    //3.判断取出来的cell是否为空,如果是空,则创建新的
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }
    NSMutableArray *array = [[DataBaseHandle shareDataBaseHandle] selectCar];
    Model *a = [array objectAtIndex:indexPath.row];
    cell.textLabel.text = a.newsTitle;;
    
    
    return cell;
}

//点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.model = [[[DataBaseHandle shareDataBaseHandle] selectCar] objectAtIndex:indexPath.row];
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    
    detailVC.model = self.model;
    
    detailVC.title = @"汽车资讯";
    [self.navigationController pushViewController:detailVC animated:YES];
    
    detailVC.myDelegate = self;
    detailVC.myDelegate2 = self;
    
    [[DataBaseHandle shareDataBaseHandle]openAndCreatDataBase];
    for (Model *model in [[DataBaseHandle shareDataBaseHandle]selectCar]) {
        //如果数据库里有点击的cell  则变量为YES(已经收藏)
        if ([model.newsTitle isEqualToString:self.model.newsTitle]) {
            
            detailVC.isCollect = YES;
            return;
        }
    }
    //如果数据库里没有点击的cell 则变量为NO(未收藏)
    detailVC.isCollect = NO;
    
}

- (void)cellect
{
    [[DataBaseHandle shareDataBaseHandle]openAndCreatDataBase];
    for (Model *model in [[DataBaseHandle shareDataBaseHandle]selectCar]) {
        if ([model.newsTitle isEqualToString:self.model.newsTitle]) {
            NSLog(@"已经收藏");
            return;
        }
    }
    [[DataBaseHandle shareDataBaseHandle]insertCar:self.model];
    
}
//detail里的取消收藏按钮方法
- (void)deleteCar
{
    [[DataBaseHandle shareDataBaseHandle]openAndCreatDataBase];
    [[DataBaseHandle shareDataBaseHandle]deleteCar:self.model];
    NSLog(@"取消收藏");
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
